
* Replication code for main analysis

* Article 	"Local Conflict Intensity and Public Perceptions of the Police: Evidence from Afghanistan"
* Journal: 	Journal of Politics 
* Authors:	Annekatrin Deglow & Ralph Sundberg
* Contact: 	annekatrin.deglow@pcr.uu.se
* Do file: 	main_analysis_ADRS.do
* Dataset:	analysis_ADRS.dta

*-------------------------------------------------------------------------------
clear
version 15 
set more off

// set working directory here 
// use "analysis_ADRS.dta"
// log using main_analysis_ADRS
*-------------------------------------------------------------------------------
* Empty ML models (to assess clustering)
*-------------------------------------------------------------------------------
foreach y in x36e_dummy x36a_dummy x34b_dummy { 
	
	// Run empty models
	melogit `y' i.year_int_cat || adm_2_num: 
	
	// Calculate the intra-class correlation coefficient
	estat icc 
}
	
*-------------------------------------------------------------------------------
* Table 1: Effect of conflict intensity on perceptions of police 
*-------------------------------------------------------------------------------
// Note: Multi-level models with random intercept at district level 

foreach y in x36e_dummy x36a_dummy x34b_dummy { 
									 		
	// Run binary models (M1, M4, M7) 
	melogit `y' abosv_sum i.year_int_cat || adm_2_num:
	
	// Run models with individual-level controls (M2, M5, M8) 
	melogit `y' abosv_sum  abosv_sumtwoyear ///
	age gender_dummy i.education i.income pashtun_dummy radio_dummy rur_urb_dummy i.ANP_corrupt ///
    i.year_int_cat || adm_2_num:
	
	// Run models with individual- and district-level controls (M3, M6, M9) 
	melogit `y' abosv_sum  abosv_sumtwoyear ///
	age gender_dummy i.education i.income pashtun_dummy radio_dummy rur_urb_dummy i.ANP_corrupt ///
    opium_adm2 adm2_ethn log_dist_pop ///
	i.year_int_cat || adm_2_num:
}

*-------------------------------------------------------------------------------
* Figure 1: Predicted probabilities 
*-------------------------------------------------------------------------------
// Note: Predict probabilities of Y=1 if conflict intensity varies from min to max, 
// while numerical controls are hold constant at mean, and categorical at mode value
// [computing takes long]

foreach y in x36e_dummy x36a_dummy x34b_dummy { 
									 		
	// Run main model (M3, M6, M9) 
	quietly melogit `y' abosv_sum  abosv_sumtwoyear age i.gender_dummy ///
	i.education i.income i.pashtun_dummy i.radio_dummy i.rur_urb_dummy i.ANP_corrupt ///
	opium_adm2 adm2_ethn log_dist_pop  i.year_int_cat || adm_2_num:
	
	// Predict probabilities
	margins,  at((means) _all gender_dummy=1 abosv_sum=(0,314)  pashtun=0 rur_urb=0 radio=1 education=1 income=4 ANP_corrupt=4 year_int_cat=2012) vsquish post

    // Graph predicted probabilities
	set scheme plotplain
	marginsplot, level(95)  recastci(rarea) recast(line)  title("") ///
	xtitle("Conflict intensity") ytitle("Predicted probability") ///
	plot1opts (lcolor(gray%80)) ///
	ci1opts(fintensity(20) fcolor(gray%70)  lcolor(gray%50) bcolor(white)) ///
	addplot(histogram abosv_sum,  legend(off) bcolor(black) width(2) ///
	start(0) ylabel(0(0.1)1) xlabel(0(50)342))  
}


*-------------------------------------------------------------------------------
* Altonji rates 
*-------------------------------------------------------------------------------
// Note: based on LPM with survey wave and district dummies and robust standard errors
 
foreach y in x36e_dummy x36a_dummy x34b_dummy { 

	// Run restricted model, save, and rename coefficients
	reg `y' abosv_sum i.year_int_cat i.adm_2_num, vce(robust)
	est store `y'_res
	gen coef_res_`y' = _b[abosv_sum]
	
	// Run full model, save, and rename coefficients
	reg `y' abosv_sum  abosv_sumtwoyear ///
	age gender_dummy i.education i.income pashtun_dummy radio_dummy rur_urb_dummy i.ANP_corrupt ///
    opium_adm2 adm2_ethn log_dist_pop ///
	i.year_int_cat i.adm_2_num, vce(robust)
	est store `y'_full
	gen coef_full_`y' = _b[abosv_sum]
	
	// Calculate Altonji rate: Full model/(restricted model - full model)
	di coef_full_`y' /(coef_res_`y'  - coef_full_`y') 
}


*-------------------------------------------------------------------------------
* Analysis without Kabul City District (largest sample size of all districts)
*-------------------------------------------------------------------------------

foreach y in x36e_dummy x36a_dummy x34b_dummy { 

	melogit `y' abosv_sum  abosv_sumtwoyear ///
	age gender_dummy i.education i.income pashtun_dummy radio_dummy rur_urb_dummy i.ANP_corrupt ///
    opium_adm2 adm2_ethn log_dist_pop ///
	i.year_int_cat if adm_2_num !=1|| adm_2_num:
}

*-------------------------------------------------------------------------------
//log close 
